package com.sh.demo.qa.module.rbac.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.sh.demo.qa.base.DynamicSpecifications;
import com.sh.demo.qa.base.SearchFilter;
import com.sh.demo.qa.base.SearchFilter.Operator;
import com.sh.demo.qa.module.rbac.dao.UserDao;
import com.sh.demo.qa.module.rbac.entity.User;

@Service
public class UserServiceImpl implements UserService{
	
	protected final static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Autowired
	private UserDao userDao;
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Page<User> findByPage(int currentPage, int pageSize, String sortType, Date startTime, Date endTime, User user) {
		PageRequest pageRequest = buildPageRequest(currentPage, pageSize, sortType);
		Specification<User> spec = buildSpecification(startTime, endTime, user);
		return userDao.findAll(spec, pageRequest);
	}
	
	/**
	 * 创建分页请求
	 */
	private PageRequest buildPageRequest(int currentPage, int pagzSize, String sortType) {
		Sort sort = null;
//		if ("auto".equals(sortType)) {
//			sort = new Sort(Direction.DESC, "id");
//		} else if ("title".equals(sortType)) {
//			sort = new Sort(Direction.ASC, "title");
//		}
		return new PageRequest(currentPage - 1, pagzSize, sort);
	}

	/**
	 * 创建动态查询条件组合
	 */
	private Specification<User> buildSpecification(Date startTime, Date endTime, User user) {
		
		List<SearchFilter> filters = new ArrayList<SearchFilter>();
		
//		// 标题过滤
//		String title = user.getTitle();
//		if(title != null && !title.trim().equals("")){
//			SearchFilter titleFilter = new SearchFilter("title", Operator.LIKE, title);
//			filters.add(titleFilter);
//		}
//		
//		// 时间过滤
//		if(startTime != null){
//			SearchFilter startTimeFilter = new SearchFilter("createtime", Operator.GTE, startTime);
//			filters.add(startTimeFilter);
//		}
//		if(endTime != null){
//			SearchFilter endTimeFilter = new SearchFilter("createtime", Operator.LTE, endTime);
//			filters.add(endTimeFilter);
//		}
		
		Specification<User> spec = DynamicSpecifications.bySearchFilter(filters, User.class);
		return spec;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public User findByCode(String account) {
		return userDao.findByCode(account);
	}


//	@Transactional(propagation=Propagation.REQUIRED)
//	public boolean addQuestion(Question info){
//		
//		try {
//			info.setCreatetime(new Date());
//			questionDao.save(info);
//		} catch (Exception e) {
//			return false;
//		}
//		return true;
//	}

}
